package com.db.person.controller;

import com.db.person.bean.Document;
import com.db.person.bean.Notice;
import com.db.person.bean.User_inf;
import com.db.person.service.IDocumentService;
import com.db.person.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

@Controller
public class DocumentController {

    @Autowired
    private IDocumentService documentService;
    
    @RequestMapping("/document/list")
    public String toList(PageModel pageModel , Model model, Integer pageIndex){

        //查询数据 service mapper  进行分页
        PageHelper.startPage(pageModel.getPageIndex(),pageModel.getPageSize());
        List<Document> all = documentService.getAll();

        PageInfo<Document> pageInfo = new PageInfo<>(all);

        List<Document> list = pageInfo.getList();

        pageModel.setRecordCount((int)pageInfo.getTotal());

        model.addAttribute("list",list);
        model.addAttribute("pageModel",pageModel);

        model.addAttribute("hre","document/list");
        return "document/list";
    }

    @RequestMapping("/document/downLoad")
    public ResponseEntity<byte[]> downLoadFile(Integer id,String oldname) throws Exception{
        File parfile = new File("D:\\personal\\down");
        //1.doc
        File[] files = parfile.listFiles();//1.doc  29.png  11.doc   tt.dd.doc    88.aa.tt.png
        for(File f : files){                             //012345678
            String fname = f.getName();
            int index = fname.lastIndexOf("."); //fname = tt.dd.doc  5
            Integer fnamePre = Integer.parseInt(fname.substring(0,index));
            if(id == fnamePre){

                FileInputStream fis = new FileInputStream(f);
                int leng = fis.available();
                byte[] buff = new byte[leng];
                fis.read(buff);

                HttpHeaders headers = new HttpHeaders();//http头信息
                String downloadFileName = new String(oldname.getBytes("UTF-8"),"ISO-8859-1");//设置编码

                headers.setContentDispositionFormData("attachment", downloadFileName);
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                //MediaType:互联网媒介类型  contentType：具体请求中的媒体类型信息
                fis.close();
                return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),headers, HttpStatus.CREATED);
            }
        }

        return null;
    }

    @RequestMapping("/document/toadd")
    public String toAdd(){
        return "document/add";
    }

    @RequestMapping("/document/add")
    public String add(Document document, MultipartFile file, HttpSession session) throws Exception{

        //1.进行入库
        String fname = file.getOriginalFilename();
        document.setFilename(fname);
        User_inf user = (User_inf) session.getAttribute("user_session");
        Integer userid = user.getId();
        document.setId(userid);
        documentService.insert(document,file);



        return "redirect:/document/list";
    }

}
